package com.sqfw.project.wp.controller;

import java.text.ParseException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.sqfw.project.wp.domain.WpInfo;
import com.sqfw.project.wp.service.IWpInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sqfw.framework.aspectj.lang.annotation.Log;
import com.sqfw.framework.aspectj.lang.enums.BusinessType;
import com.sqfw.project.wp.domain.WpInfoReceive;
import com.sqfw.project.wp.service.IWpInfoReceiveService;
import com.sqfw.framework.web.controller.BaseController;
import com.sqfw.framework.web.domain.AjaxResult;
import com.sqfw.common.utils.poi.ExcelUtil;
import com.sqfw.framework.web.page.TableDataInfo;

/**
 * 12345工单表Controller
 *
 * @author lx
 * @date 2023-07-05
 */
@RestController
@RequestMapping("/wp/wpInfoReceive")
public class WpInfoReceiveController extends BaseController {
    @Autowired
    private IWpInfoReceiveService wpInfoReceiveService;

    @Autowired
    private IWpInfoService wpInfoService;

    /**
     * 查询12345工单表列表
     */
    @PreAuthorize("@ss.hasPermi('wp:wpInfoReceive')")
    @GetMapping("/getWpInfoReceiveList")
    public TableDataInfo getWpInfoReceiveList(WpInfoReceive wpInfoReceive) {
        startPage();
        return wpInfoReceiveService.selectWpInfoReceiveList(wpInfoReceive);
    }

    /**
     * 导出12345工单表列表
     */
    @Log(title = "12345工单表", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('wp:wpInfoReceive')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, WpInfoReceive wpInfoReceive) {
       // List<WpInfoReceive> list = wpInfoReceiveService.selectWpInfoReceiveList(wpInfoReceive);
        /*TableDataInfo tableDataInfo = wpInfoReceiveService.selectWpInfoReceiveList(wpInfoReceive);
        List<WpInfoReceive> rows = (List<WpInfoReceive>) tableDataInfo.getRows();*/

       List<WpInfoReceive>  list = wpInfoReceiveService.export(wpInfoReceive);
        ExcelUtil<WpInfoReceive> util = new ExcelUtil<WpInfoReceive>(WpInfoReceive.class);
        util.exportExcel(response, list, "12345工单表数据");
    }

    /**
     * 获取12345工单表详细信息
     */
    @PreAuthorize("@ss.hasPermi('wp:wpInfoReceive')")
    @GetMapping(value = "/{scid}")
    public AjaxResult getInfo(@PathVariable("scid") String scid) {
        return success(wpInfoReceiveService.selectWpInfoReceiveByScid(scid));
    }


    /**
     * 12345工单分配
     */
    @Log(title = "12345工单表", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('wp:wpInfoReceive')")
    @PostMapping("/allocationEdit")
    public synchronized AjaxResult allocationEdit(@RequestBody WpInfoReceive wpInfoReceive) throws ParseException {

        WpInfoReceive infoReceive = wpInfoReceiveService.selectWpInfoReceiveByScid(wpInfoReceive.getScid());

        WpInfo info = new WpInfo();
        info.setTsWpid(infoReceive.getTsWpid());
        info.setStarttime(infoReceive.getStarttime());

        Integer count= wpInfoService.countTsWpid(info);
        if (count > 0) {

            return AjaxResult.error("该12345工单已经分配过");
        }

        String wpId = wpInfoService.getWpId("SQ");
        infoReceive.setWpid(wpId);
        infoReceive.setSender(wpInfoReceive.getSender());
        return toAjax(wpInfoReceiveService.updateWpInfoReceive(infoReceive));
    }

    /**
     * 批量收回12345工单
     */
    @Log(title = "批量收回12345工单", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('wp:wpInfoReceive')")
    @PostMapping("/updateWpInfoReceiveList")
    public AjaxResult updateWpInfoReceiveList(@RequestBody WpInfoReceive wpInfoReceive) {
        if (CollectionUtils.isEmpty(wpInfoReceive.getScidList())) {
            return AjaxResult.error("批量收回12345工单scidList没有传递");
        }
        if (StringUtils.isBlank(wpInfoReceive.getStatus())) {
            return AjaxResult.error("批量收回12345工单status没有传递");
        }
        int count = wpInfoReceiveService.updateWpInfoReceiveList(wpInfoReceive);
        return AjaxResult.success("收回成功数量：" + count);
    }


}
